package com.etl.dataflow.executor.element;

import com.etl.dataflow.common.util.ClassSize;
import lombok.ToString;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.sql.Date;
import java.sql.Timestamp;

/**
 * @author dx
 * @since 2022/4/14
 */
@ToString(callSuper = true)
public class BytesColumn extends Column<byte[]> {

    public BytesColumn(String name, byte[] value, int jdbcType, String jdbcTypeName) {
        this(name, value, jdbcType, jdbcTypeName, StandardCharsets.UTF_8);
    }

    public BytesColumn(String name, byte[] value, int jdbcType, String jdbcTypeName, Charset charset) {
        super(name, value, jdbcType, jdbcTypeName, charset);
    }

    @Override
    public String asString() {
        return super.value == null ? null : new String(super.getValue(), super.charset);
    }

    @Override
    public byte[] asBytes() {
        return super.getValue();
    }

    @Override
    public Timestamp asTimestamp() {
        return null;
    }

    @Override
    public Date asDate() {
        return null;
    }

    @Override
    public int getByteSize() {
        return ClassSize.align(ClassSize.ARRAY + (super.value == null ? 0 : super.value.length));
    }
}
